<?php
class userMod extends commonMod
{
	public function __construct()
	{
		parent::__construct();
	}

	//用户列表
	public function get_list()
	{
		$order=in(isset($_GET[0]) ? trim($_GET[0]) :'id');
		$group_id=in(isset($_GET[1]) ? trim($_GET[1]) :'all');
		//分页
		$url=__URL__.'/get_list-'.$order.'-'.$group_id.'-page-{page}.html';
		$list_rows=10;
		if($group_id=='all')
		{
			$count=$this->model->table('user')->where('is_validated=1  AND group_id!=1')->count();
			$this->assign('list',$this->condition_list($list_rows,$cur_page,'is_validated=1  AND group_id!=1',$order));
			}
		else
		{
			$count=$this->model->table('user')->where("group_id ='$group_id'")->count();
			$this->assign('list',$this->condition_list($list_rows,$cur_page,"group_id ='$group_id' AND is_validated=1  AND group_id!=1",$order));
			}
		$this->assign('group',$this->group());
		$page=new Page();
		$cur_page=$page->getCurPage();
		$this->assign('page',$this->page($url,$count,$list_rows));
		$this->assign('lang',LANG::getPack('user'));
		$this->display('user/list.html');
	}
	//审核列表
	public function vdt_list()
	{
		$order=in(isset($_GET[0]) ? trim($_GET[0]) :'id');
		//分页
		$url=__URL__.'/get_list-'.$order.'-'.$group_id.'-page-{page}.html';
		$list_rows=10;
		$count=$this->model->table('user')->where('is_validated=0  AND group_id!=1')->count();
		$this->assign('list',$this->condition_list($list_rows,$cur_page,'is_validated=0  AND group_id!=1',$order));
		$this->assign('user_is_validated',$this->config['USER_IS_VALIDATED']);
		$page=new Page();
		$cur_page=$page->getCurPage();
		$this->assign('page',$this->page($url,$count,$list_rows));
		$this->assign('lang',LANG::getPack('user'));
		$this->display('user/vdt_list.html');
		}
	//添加
	public function add()
	{
		if(empty($_POST['do']))
		{
			$this->assign('group',$this->group());
			$this->assign('lang',LANG::getPack('user'));
			$this->display('user/add.html');
			return;
			}
		$data=array();
		$data['name']=in($_POST['name']);
		$data['pwd']=md5(in($_POST['pwd']));
		$data['group_id']=in($_POST['group_id']);
		if(empty($data['name']))
		{
			$this->error('用户名不可为空！');
			}
		if(empty($data['pwd']))
		{
			$this->error('用户密码不可为空！');
			}
		if($this->config['USER_IS_VALIDATED'])
		{
			$data['is_validated']=1;
			}
		if($this->model->table('user')->data($data)->insert())
		{
			$this->success('添加成功！','/get_list');
			}
		else
		{
			$this->error('添加失败！');
			}
		}
	//编辑
	public function edit()
	{
		if(empty($_POST['do']))
		{
			$id=intval($_GET[0]);
			$this->assign('read',$this->read($id));
			$this->assign('group',$this->group("parent_id =1"));
			$this->assign('lang',LANG::getPack('user'));
			$this->display('user/edit.html');
			return;
			}
		$data=array();
		$data['name']=in($_POST['name']);
		$data['pwd']=md5(in($_POST['pwd']));
		$data['group_id']=in($_POST['group_id']);
		if(empty($data['name']))
		{
			$this->error('用户名不可为空！');
			}
		if(empty($data['pwd']))
		{
			$this->error('用户密码不可为空！');
			}
		$condition['id']=intval($_POST['id']);
		//md5以加密需重设密码才可修改
		$read=$this->read(intval($_POST['id']));
		if(in($_POST['pwd'])==$read['pwd'])
		{
			$this->error('请重设密码！');
			}
		if($this->model->table('user')->data($data)->where($condition)->update())
		{
			$this->success('修改成功！','/get_list');
			}
		else
		{
			$this->error('修改失败！或您未修改任何数据！');
			}
		}
	//密码设置
	public function edit_pwd()
	{
		if(empty($_POST['do_pwd']))
		{
			//$this->assign('lang',LANG::getPack('user'));

			$this->display('user/edit_pwd');
			return;
		}
		$data=array();
		$oldpwd=in($_POST['oldpwd']);
		$user_id=$_SESSION['user_id'];
		$read=$this->read($user_id);
		//echo md5($oldpwd);
		//echo "<br>";
		//echo $redad["user_password"];
		// echo "<br>";

		//dump($read);
		if(md5($oldpwd)!=$read['user_password'])
		{
			$this->error('原密码输入错误！');
		}
	 	if($_POST['newpwd']==$_POST['newpwd2']&&!empty($_POST['newpwd']))
		{
			$data['user_password']=md5(in($_POST['newpwd'])) ;
		}
		else
		{
			$this->error('两次输入的密码不一致，或者新密码为空！');
		}
		$condition['user_id']=$user_id;

		//urldizhi==http://127.0.0.5/admin/index.php/user
	 	if($this->model->table('users')->data($data)->where($condition)->update())
		{
			//echo $this->model->sql;
			$this->success('密码设置成功！','/edit_pwd');
		}
		else
		{
			$this->error('密码设置失败！或您未修改任何数据！');
		}
	}
	//个人资料
	public function edit_all()
	{
		if(empty($_POST['do_do']))
		{
			$id=$_SESSION['user_id'];
			$read=$this->read($id);


			if($read['parent_id'])
			{
				$info=$this->read($read['parent_id']);

				$read['parent_id']=$info['name'];
			}
			else
			{
				$read['parent_id']='无';
			}
			$this->assign('read',$read);
			$this->assign('group',$this->group("parent_id =1"));
			//$this->assign('lang',LANG::getPack('user'));
			$this->display('user/edit_all');
			return;
		}
		$data=array();
		$data['sex']=in($_POST['sex']);
		$data['tel']=in($_POST['tel']);
		$data['phone']=in($_POST['phone']);
		$data['identity']=in($_POST['identity']);
		$data['qq']=in($_POST['qq']);
		$data['birthday']=in($_POST['birthday']);
		$data['email']=in($_POST['email']);
		$data['msn']=in($_POST['msn']);
		$data['address']=in($_POST['address']);
		$data['last_time']=time();
		if($_FILES['photo']['name']!="")
		{
			$uploadpath='../upload/user/'; 	//设置上传文件的路径
			$imgpath='/upload/user/'; 		//设置文件的路径
			if($imgupload=$this->_upload($uploadpath))
			{
				$data['photo']=$imgpath.$imgupload[0]['savename'];
			}
		}
		if(empty($data['qq']))
		{
			$this->error('QQ不可为空！');
		}
		if(empty($data['email']))
		{
			$this->error('用户邮箱不可为空！');
		}
		$condition['id']=$_SESSION['user_id'];
		if($this->model->table('users')->data($data)->where($condition)->update())
		{
			$this->success('设置成功！','/edit_all');
		}
		else
		{
			$this->error('设置失败！或您未修改任何数据！');
		}
	}
	//详细
	public function detail()
	{
		if(empty($_POST['do']))
		{
			$id=intval($_GET[0]);
			$read=$this->read($id);
			$read['birthday']=date('y-m-d h:i:s',$read['birthday']);
			$read['reg_time']=date('y-m-d h:i:s',$read['reg_time']);
			$read['last_login']=date('y-m-d h:i:s',$read['last_login']);
			$read['last_time']=date('y-m-d h:i:s',$read['last_time']);
			if($read['sex'])
			{
				$read['sex']='男';
				}
			else
			{
				$read['sex']='女';
				}
			if($read['is_validated'])
			{
				$read['is_validated']='是';
				}
			else
			{
				$read['is_validated']='否';
				}
			if($read['parent_id'])
			{
				$info=$this->read($read['parent_id']);
				$read['parent_id']=$info['name'];
				}
			else
			{
				$read['parent_id']='无';
				}
			$this->assign('read',$read);
			$this->assign('lang',LANG::getPack('user'));
			$this->display('user/detail.html');
			return;
			}
		echo "<script type=\"text/javascript\">history.go(-2);</script>";
		}
	//审核
	public function is_vdt()
	{
		$condition['id']=intval($_GET[0]);
		$data['is_validated']=1;
		if($this->model->table('user')->data($data)->where($condition)->update())
		{
			$this->success('审核通过！','/vdt_list');
			}
		else
		{
			$this->error('审核失败！');
			}
		}
	public function allvdt()
	{
		$id=$_POST['id'];
		$new_id = substr($id,0,strlen($id)-1);
		$data['is_validated']=1;
		if($this->model->table('user')->data($data)->where('id in ('.$new_id.')')->update())
		{
			echo 1;
			}
		else
		{
			echo 0;
			}
		}
	//读取
	public function read($id)
	{
		$condition['user_id']=$id;
		//echo $id;
		$read=$this->model->table('users')->where($condition)->find();
		if($read)
		{
			return $read;
		}
		else
		{
			$this->error('不存在此用户！');
		}
	}
	//删除
	public function del()
	{
		$condition['id']=intval($_GET[0]);
		$read=$this->read(intval($_GET[0]));
		if($this->model->table('user')->where($condition)->delete())
		{
			if(is_file('..'.$read['photo']))
			{
				unlink('..'.$read['photo']);
				}
			$this->success('删除成功！','/get_list');
			}
		else
		{
			$this->error('删除失败！');
			}
		}
	//查询列表
	public function condition_list($list_rows,$cur_page,$where,$order)
	{
		$limit_start=($cur_page-1)*$list_rows;
		$limit=$limit_start.','.$list_rows;
		$list=$this->model->field('id,name,email,qq,phone,sex,address,is_validated')->table('user')->where($where)->order($order.' DESC')->select();
		if($list)
		{
			foreach($list as & $val)
			{
				$val['qq']="<a target=\"_blank\" href=\"http://wpa.qq.com/msgrd?v=3&uin=".$val['qq']."&site=qq&menu=yes\"><img border=\"0\" src=\"http://wpa.qq.com/pa?p=2:".$val['qq'].":45\" alt=\"Lqcms\" title=\"Lqcms\"></a> ".$val['qq'];
				if($val['sex'])
				{
					$val['sex']="男";
					}
				else
				{
					$val['sex']="女";
					}
				$val['edit']=__URL__.'/edit-'.$val['id'].'.html';
				$val['del']=__URL__.'/del-'.$val['id'].'.html';
				}
			return $list;
				}
		}
	//用户分类
	public function group()
	{
		$list=$this->model->field('id,name')->table('group')->where('id!=1')->select();
		if($list)
		{
			foreach($list as & $val)
			{
				$val['url']=__URL__.'/get_list-id-'.$val['id'].'-page-1.html';
				}
			return $list;
				}
		}
	//删除所选
	public function alldel()
	{
		$id=$_POST['id'];
		$new_id = substr($id,0,strlen($id)-1);
		$data=explode(',',$new_id);
		for($i=0;$i<count($data);$i++)
		{
			$read=$this->read(intval($data[$i]));
			//删除图片
			if(is_file('..'.$read['photo']))
			{
				unlink('..'.$read['photo']);
				}
			}
		if($this->model->table('user')->where('id in ('.$new_id.')')->delete())
		{
			echo 1;
			}
		else
		{
			echo 0;
			}
		}
}
?>